/**
 *    TimedProfiles - let Android perform tasks based on conditions
 *    Copyright (C) 2011  Daniel Weinhold
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * */
package de.drak.Profiles;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/** simple adapter for shortcut and activity picker */
public class ListAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	
	private CharSequence[] db_name = null;
	private CharSequence[] db_text = null;
	private long[] db_id = null;
	private Drawable[] db_icon = null;
	
	
	private static class ViewHolder {
		 TextView text;
		 TextView text2;
		 ImageView icon;
	 }
	 
	 public ListAdapter(Context context, long[] id, CharSequence[] name, CharSequence[] text, Drawable[] icon) {
	 mInflater = LayoutInflater.from(context);

	 
		//new button
		db_id = id;
		db_text = text;
		db_name = name;
		db_icon = icon;
	 }

	 public int getCount() {
	 return db_id.length;
	 }

	 public Object getItem(int position) {
		 return position;
	 }

	 public long getItemId(int position) {
		 return db_id[position];
	 }
	 @Override
	 public View getView(int position, View convertView, ViewGroup parent) {
		 ViewHolder holder;
		 if (convertView == null) {
			 convertView = mInflater.inflate(R.layout.row, null);
			 holder = new ViewHolder();
			 
			 holder.text = (TextView) convertView.findViewById(R.id.firstLine);
			 holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			 holder.text2 = (TextView) convertView.findViewById(R.id.secondLine);
			 
			 convertView.setTag(holder);
		 } else {
			 holder = (ViewHolder) convertView.getTag();
		 }


		 
		 holder.icon.setImageDrawable(db_icon[position]);
		 holder.text.setText(db_name[position]);
		 
		 if(db_text!=null){
			 if(db_text[position] == null || db_text[position].length() < 1)
			 {
				 holder.text2.setVisibility(View.GONE);
			 }else{
				 holder.text2.setVisibility(View.VISIBLE);
				 holder.text2.setText(db_text[position]);				 
			 } 
		 }else{
			 holder.text2.setVisibility(View.GONE);
		 }
	
		 return convertView;
	 }
}